package com.csw.flink.transformation

import org.apache.flink.streaming.api.scala._
import org.apache.flink.streaming.api.windowing.time.Time
import org.apache.flink.streaming.api.windowing.windows.TimeWindow

object Demo06Window {
  def main(args: Array[String]): Unit = {

    val env: StreamExecutionEnvironment = StreamExecutionEnvironment.getExecutionEnvironment

    env.setParallelism(1)

    val ds: DataStream[String] = env.socketTextStream("master",8888)

    val keyByDS: KeyedStream[(String, Int), String] = ds.flatMap(_.split(",")).map((_,1)).keyBy(_._1)


    //时间滚动窗口   统计5秒内的WordCount
     val ds1: WindowedStream[(String, Int), String, TimeWindow] = keyByDS.timeWindow(Time.seconds(5))

    val ds2: DataStream[(String, Int)] = ds1.sum(1)


    ds2.print()

    env.execute()
  }
}
